package com.topvision.myviewtest.gscview.ball;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.DashPathEffect;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Point;
import android.graphics.Typeface;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;

/**
 * @author lucy
 * @date 2019-01-29 11:25
 * @description //绘制坐标系
 */

public class CoordinateSystemView extends View {
    /**
     * 实线
     */
    private Paint mSolidPaint;
    /**
     * 虚线
     */
    private Paint mDashPaint;
    /**
     * 刻度
     */
    private Paint mTextPaint;
    /**
     * 屏幕尺寸大小
     */
    private Point mScreenSize;

    public CoordinateSystemView(Context context) {
        this(context, null);
    }

    public CoordinateSystemView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public CoordinateSystemView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        // 虚线
        mDashPaint = new Paint();
        mDashPaint.setColor(Color.GRAY);
        mDashPaint.setAntiAlias(true);
        mDashPaint.setStrokeWidth(2);
        mDashPaint.setStyle(Paint.Style.STROKE);
        //绘制长度为10的实线后再绘制长度为5的空白区域，0位间隔
        mDashPaint.setPathEffect(new DashPathEffect(new float[]{10, 5}, 0));

        // 实线
        mSolidPaint = new Paint();
        mSolidPaint.setAntiAlias(true);
        mSolidPaint.setColor(Color.BLACK);
        mSolidPaint.setStrokeWidth(5);
        mSolidPaint.setStyle(Paint.Style.STROKE);

        // 刻度文本
        mTextPaint = new Paint();
        mTextPaint.setAntiAlias(true);
        mTextPaint.setColor(Color.BLACK);
        mTextPaint.setTextSize(24);
        mTextPaint.setStrokeWidth(10);
        mTextPaint.setTypeface(Typeface.DEFAULT_BOLD);

        // 获取屏幕尺寸
        mScreenSize = UIHelper.getScreenSize(getContext());
    }

    @Override
    public void draw(Canvas canvas) {
        super.draw(canvas);
        // 画布平移屏幕中心点
        int width = mScreenSize.x / 2;
        int height = mScreenSize.y / 2;
        canvas.translate(width, height);

        // 画x轴
        Path solidPathX = new Path();
        solidPathX.moveTo(-mScreenSize.x / 2, 0);
        solidPathX.lineTo(width, 0);
        // 画x轴箭头指向
        solidPathX.lineTo(width - 40, -10);
        solidPathX.moveTo(width, 0);
        solidPathX.lineTo(width - 40, 10);
        // 画y轴
        Path solidPathY = new Path();
        solidPathY.moveTo(0, height);
        solidPathY.lineTo(0, -height);
        // 画y轴箭头指向
        solidPathY.lineTo(-10, -height + 40);
        solidPathY.moveTo(0, -height);
        solidPathY.lineTo(10, -height + 40);

        canvas.drawPath(solidPathX, mSolidPaint);
        canvas.drawPath(solidPathY, mSolidPaint);

        // 画虚线 x轴
        Path dashPathX = new Path();
        // 计算可以画多少根虚线
        int countX = mScreenSize.x / 100;
        // 画虚线+刻度值
        for (int i = 0; i < countX; i++) {
            int x = 50 * (i + 1);
            // 虚线
            dashPathX.moveTo(x, -height);
            dashPathX.lineTo(x, height);
            canvas.drawPath(dashPathX, mDashPaint);
            dashPathX.moveTo(-x, -height);
            dashPathX.lineTo(-x, height);
            canvas.drawPath(dashPathX, mDashPaint);
            // 刻度值
            solidPathY.moveTo(x, 0);
            solidPathY.lineTo(x, -15);
            canvas.drawPath(solidPathY, mSolidPaint);
            solidPathY.moveTo(-x, 0);
            solidPathY.lineTo(-x, -15);
            canvas.drawPath(solidPathY, mSolidPaint);
            // 画刻度值(文本居中)
            if ((i+1) % 2 == 0) {
                // 文字宽
                float textWidth = mTextPaint.measureText(String.valueOf(x));
                // 文字baseline在y轴方向的位置
                float baseLineY = Math.abs(mTextPaint.ascent() + mTextPaint.descent()) / 2;
                canvas.drawText(String.valueOf(x), x - textWidth / 2, baseLineY + 20, mTextPaint);
                canvas.drawText(String.valueOf(-x), -x - textWidth / 2, baseLineY + 20, mTextPaint);
            }
        }
        // 画虚线 y轴
        Path dashPathY = new Path();
        // 计算可以画多少根虚线
        int countY = mScreenSize.y / 100;
        // 画虚线+刻度值
        for (int i = 0; i < countY; i++) {
            int y = 50 * (i + 1);
            dashPathY.moveTo(-width, y);
            dashPathY.lineTo(width, y);
            canvas.drawPath(dashPathY, mDashPaint);
            dashPathY.moveTo(-width, -y);
            dashPathY.lineTo(width, -y);
            canvas.drawPath(dashPathY, mDashPaint);
            // 刻度值
            solidPathX.moveTo(0, y);
            solidPathX.lineTo(15, y);
            canvas.drawPath(solidPathX, mSolidPaint);
            solidPathX.moveTo(0, -y);
            solidPathX.lineTo(15, -y);
            canvas.drawPath(solidPathX, mSolidPaint);
            // 画刻度值(文本居中)
            if ((i+1) % 2 == 0) {
                // 文字baseline在y轴方向的位置
                float baseLineY = Math.abs(mTextPaint.ascent() + mTextPaint.descent()) / 2;
                canvas.drawText(String.valueOf(-y), 20, -y + baseLineY, mTextPaint);
                canvas.drawText(String.valueOf(y), 20, y + baseLineY, mTextPaint);
            }
        }
    }
}
